package com.api.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ServiceRequestImp implements ServiceRequest{

	private Service service;
	
	private ApiClient apiClient;
	
	private Map<String, Object> pathParams = new HashMap<String, Object>();
	
	private Map<String, Object> params = new HashMap<String, Object>();
	
	ServiceRequestImp(ApiClient apiClient, Service service){
		
		this.service = service;
		this.apiClient = apiClient;
	}
	
	public Service getService() {
		return service;
	}

	public ServiceRequest setParameter(String name, Object value) {
		
		VariablePath servicePath = service.getPath();
		
		List<String> pathVars = servicePath.getVariables();
		List<String> requiredParams = service.getRequiredParameters();
		List<String> optionalParams = service.getOptionalParameters();
		
		if(pathVars.contains(name)){
		
			pathParams.put(name, value);
		}
		else if(requiredParams.contains(name) || optionalParams.contains(name)){
			
			params.put(name, value);
		}
		else{
			throw new IllegalArgumentException(String.format("Parameter '%s' does not exists in service '%s'", name, service.getPath()));
		}
		return this;
	}

	public <T> T send() {
		
		return apiClient.send(service, pathParams, params);
	}
}
